<?php

namespace App\Admin\Controllers;

use App\Models\Book;
use App\Models\BorrowBook;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BorrowBookController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BorrowBook(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function ($d){
                return $d ? User::find($d)->nickname : '';
            });
            $grid->column('book_id')->display(function ($d){
                return $d ? Book::find($d)->title : '';
            });
            $grid->column('borrow_date');
            $grid->column('return_date');
            $grid->column('illegal');
            $grid->column('manage_id')->display(function ($d){
                return $d ? User::find($d)->nickname : '';
            });
            $grid->column('status')->display(function ($d){
                return BorrowBook::STATUS_ENUM[$d];
            });
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('user_id')->select(User::all()->pluck('nickname', 'id'));

            });
            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BorrowBook(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('book_id');
            $show->field('borrow_date');
            $show->field('end_date');
            $show->field('return_date');
            $show->field('illegal');
            $show->field('manage_id');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BorrowBook(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('book_id');
            $form->text('borrow_date');
            $form->text('end_date');
            $form->text('return_date');
            $form->text('illegal');
            $form->text('manage_id');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
